package org.example.demo21.util;

import jakarta.validation.constraints.NotNull;
import org.example.demo21.config.RabbitDelayConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 *
 * @author guocong
 * @since 2024/4/22 13:39
 */
@Component
public class RabbitDelayUtil {


    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 延迟发送
     *
     * @param routingKey 路由键
     * @param msg 消息
     * @param delay 延迟
     * @return {@link Boolean}
     * @throws AmqpException AMQP 异常
     */
    public Boolean sendDelay(@NotNull String routingKey, @NotNull Object msg, @NotNull Long delay) throws AmqpException {
        rabbitTemplate.convertAndSend(RabbitDelayConfig.DELAYED_EXCHANGE_NAME, routingKey, msg, message -> {
            message.getMessageProperties().setHeader("x-delay", delay);
            return message;
        });
        return true;

    }


}
